package com.imoblife.brainwavestus;

import com.google.analytics.tracking.android.EasyTracker;
import com.imoblife.brainwavestus.customview.MenuList;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.obpurchaseSystem.OBpurchaseSystem;
import com.imoblife.brainwavestus.obpurchaseSystem.TusPointManager;
import com.imoblife.brainwavestus.receiver.UpdateTitleViewReceiver;
import com.imoblife.brainwavestus.util.DialogUtil;
import com.imoblife.brainwavestus.util.NetUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {
	
	public static final String TAG = "BaseActivity";
	
	public ActivityBuffer mActivityBuffer;
	
	public BroadcastReceiver mUpdateTitleViewReceiver;
	
	public String mTitleName;
	
	public boolean isPad;
	
	protected void onCreate(Bundle arg0) {
		
		super.onCreate(arg0);
		
		BrainWavestusApplication application 
			= (BrainWavestusApplication)getApplication();
		isPad = application.isPad();

		setRequestedOrientation();

		mActivityBuffer = ActivityBuffer.getInstance();
		
		mActivityBuffer.addActivity(this);
	}

	protected void onStart() {

		EasyTracker.getInstance().activityStart(this); // Add this method.
		
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		
		super.onResume();
		
		if(isNeedRegisterReceiver()) {
			
			registerUpdateTitleViewReceiver();
		}
		
		if(isNeedSetTitleName()) {
			
			setTitleName();
		}
		
	}
	
	public BrainWavestusApplication getBrainWavesApplication() {
		
		BrainWavestusApplication bwApplication 
			= (BrainWavestusApplication)this.getApplication();
		
		return bwApplication;
	}
	
	public void setTitleName() {
		
		TextView tvTitleName = (TextView) findViewById(R.id.title_name);
		
		if(tvTitleName!= null){
			
			tvTitleName.setText(mTitleName);	
		}		
	}
	
	public void initTitleName(String titleName) {
		
		mTitleName = titleName;
	}
	
	protected void onStop() {
		
		EasyTracker.getInstance().activityStop(this); // Add this method.
		
		super.onStop();
	}

	protected void onDestroy() {
		
		mActivityBuffer.remove(this);
		
		super.onDestroy();
		
		if(isNeedRegisterReceiver()) {
			
			unregisterUpdateTitleViewReceiver();
		}
	}
	
	public void setRequestedOrientation() {

		// 如果是平板则设置为横屏
		if (isPad) {

			if (this.getRequestedOrientation() 
					!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
				this.setRequestedOrientation(
						ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			}

			// 否则为竖屏
		} else {

			if (this.getRequestedOrientation() 
					!= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
				this
						.setRequestedOrientation(
								ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			}
		}
	}


	public void homeFinish() {
		
		super.finish();
	}
	
	public void clearStackActivity(){
		
		mActivityBuffer.clear();
	}
	
	public void onImgHomeClicked(View v) {
		
		this.finish();		
	}
	
	
	public boolean isNeedRegisterReceiver() {
		
		return true;
	}
	
	public boolean isNeedSetTitleName() {
		
		return true;
	}
	
	//暂时先去掉title view的广播注册
	public void registerUpdateTitleViewReceiver(){
		
//		ImageButton subScriptionButton;
//		
//		ImageButton mySubScriptionButton;
//		
//		subScriptionButton = (ImageButton) this
//				.findViewById(R.id.subscription);
//		
//		mySubScriptionButton = (ImageButton) this
//				.findViewById(R.id.my_subscription);
//		
//		mUpdateTitleViewReceiver = 
//				new UpdateTitleViewReceiver(subScriptionButton,mySubScriptionButton);
//		
//		IntentFilter filter = new IntentFilter();
//		filter.addAction(Constant.UPDATE_STATEVIEW_SUBSCRIPTION);
//		filter.addAction(Constant.UPDATE_STATEVIEW_MYSUBSCRIPTION);
//		
//		registerReceiver(mUpdateTitleViewReceiver, filter);
	}
	
	public void unregisterUpdateTitleViewReceiver(){
		
//		if(isNeedRegisterReceiver() && (mUpdateTitleViewReceiver != null)) {
//			
//			unregisterReceiver(mUpdateTitleViewReceiver);
//			
//			mUpdateTitleViewReceiver = null;
//		}
	}
	
	public void onTitleMenuClicked(View v) {
		
		findViewById(R.id.menu).setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				MenuList menuList = new MenuList(v, BaseActivity.this);

				menuList.addItem(
						getString(R.string.tus_credits)
								+ OBpurchaseSystem.getOBpurchaseSystem(
										BaseActivity.this).getPoints(),
						new Runnable() {

							public void run() {
								OBpurchaseSystem.getOBpurchaseSystem(
										BaseActivity.this).showOffers();

							}
						});

				menuList.addItem(getString(R.string.getCredits),
						new Runnable() {

							public void run() {
								new TusPointManager(BaseActivity.this);
							}
						});
				menuList.display();
			}
		});

	}
	
	public void onSubScriptionClicked(View v) {
		
		if(!NetUtil.isConnectedNetwork(this)) {
			Toast.makeText(this,
					getString(R.string.register_error_code_net),
					Toast.LENGTH_LONG).show();
			return;
		}
		
		DialogUtil.showSubscriptionDialog(this);
	}

	public void onMySubScriptionClicked(View v) {
		
		DialogUtil.showMySubscriptionDialog(this);
	}
}
